﻿/*******************************************************************************
 * AAEngine
 * Copyright (c) 2010 Mike Jarosch
 *
 * Original source PushButton Engine:
 * Copyright (C) 2009 PushButton Labs, LLC
 * For more information see http://www.pushbuttonengine.com
 *
 * This file is licensed under the terms of the MIT license, which is included
 * in the "MIT License.txt" file at the root directory of this SDK.
 ******************************************************************************/

using AAEngine.Engine.Core;
using AAEngine.Engine.Entities;

namespace AAEngine.Engine.Components
{
    public class TickedComponent : EntityComponent, ITickedObject
    {
        bool _registerForUpdates = true;
        bool _isRegisteredForUpdates = false;

        public int UpdatePriorityNumber { get; set; }

        public bool RegisterForUpdates
        {
            get
            {
                return _registerForUpdates;
            }
            set
            {
                _registerForUpdates = value;

                if (_registerForUpdates && !_isRegisteredForUpdates)
                {
                    // Need to register.
                    _isRegisteredForUpdates = true;

                    AAGame.ProcessManager.AddTickedObject(this, UpdatePriorityNumber);
                }
                else if (!_registerForUpdates && _isRegisteredForUpdates)
                {
                    // Need to unregister.
                    _isRegisteredForUpdates = false;

                    AAGame.ProcessManager.RemoveTickedObject(this);
                }
            }
        }

        public override void OnAdd()
        {
            base.OnAdd();

            RegisterForUpdates = true;
        }

        public override void OnRemove()
        {
            RegisterForUpdates = false;

            base.OnRemove();
        }

        public virtual void OnTick(float deltaTime) { }
    }
}
